LZW (short for Lempel-Ziv-Welch) is a lossless compression technique that is an integral part of the GIF image file format and an option in TIFF image files.
The Lempel-Ziv algorithm looks for repeated pixel combinations and builds a lookup table (on-the-fly) of compression codes to represent the combinations. The codes are Huffman-encoded for further compression efficiency.
LZW compression is commonly used for 1- through 8-bit palette color images and less often for 24-bit RGB images. Typically an LZW-compressed palette color image is 60 to 80% of the original size. But for some images LZW can create a larger file than the original uncompressed image requires. Compression efficiency varies based on image complexity.
Table 1. Comparison of Compression Efficiencies for a 24-bit Image Compression Image bits/pixel File Format Size (kb) (% of original) none 24 TIFF 223,646 100 LZW 24 TIFF 243,829 109 run length 24 TGA 224,055 101 JPEG quality=75 24 JPEG 14,572 7 JPEG quality=50 24 JPEG 9,413 4Notice that for the 24-bit image LZW (or run length encoding) is not really compressing but actually increasing the space requirements. This demonstrates why LZW compression is seldom used for 24-bit images. (Be sure to see the JPEG Application Note.)
Using color reduction (with optimized palette and diffusion scatter) the original image is converted to 8 bits per pixel. Color reduction cuts the image size by 2/3. Now examine the resulting file sizes when the image is saved in 8-bit formats.
Table 2. Comparison of Compression Efficiencies for an 8-bit Image Compression Image bits/pixel File Format Size (kb) (% of original) none 8 TIFF 77,810 100 run length 8 PCX 79,028 102 LZW 8 GIF 59,582 77Table 2 shows typical LZW compression efficiency for palette color images. While it is not always true, at least in this case LZW would not be the compression of choice. (To compare the image quality and speed of transmission of LZW vs. JPEG visit our website, www.catenary.com/victor and jump to this same application note. The relevent files are dendrob.gif and qual50.jpg.)
The Victor Library includes complete support for LZW compression and decompression within the GIF and TIFF-LZW image file formats. Unfortunately, due to the license agreement imposed by Unisys this LZW functionality is locked until you provide proof of a valid LZW software license agreement with Unisys.
Unisys holds the patent rights to the LZW compression technology used in GIF and TIFF-LZW image files. The LZW license agreement is necessary if you wish to use this technology. This is true regardless of how you implement LZW support -- whatever library you use or even if you write all the code yourself. Visit the Unisys website (www.unisys.com) for further information. The license generally requires modest royalty payments which will not cause financial hardship to any developer.
The Victor Library includes complete support for LZW compression and decompression within the GIF and TIFF-LZW image file formats. Table 3 lists the relevant functions.
Table 3. Victor Functions Supporting LZW Compression and Decompression Victor Function File Format Comments loadtif TIFF 8- and 24-bit images may be LZW-compressed loadtifpage TIFF 8- and 24-bit images may be LZW-compressed savetif TIFF LZW is an option for 8- and 24-bit images savetifpage TIFF LZW is an option for 8- and 24-bit images loadgif GIF all GIF images are LZW compressed savegif GIF 1- and 8-bit images are LZW compressed savegifex GIF 1-, 4-, and 8-bit images are LZW compressed with interlace and transparency optionsThe TIFF functions all perform normally for non-LZW compressed images, that is, no unlocking is necessary.
int load_a_gif(char *fname, imgdes *image) { GifData gdat; int rcode; // Get info on the GIF file we're to load gifinfo(fname, &gdat); // Allocate space for the image based on the data in the GIF file allocimage(image, gdat.width, gdat.length, gdat.vbitcount); rcode = loadgif(fname, image); // Assumes LZW functionality previously unlocked return(rcode); }As soon as the image is loaded it is eligible for use with any of the Victor functions for processing, printing, saving, or display. See the list of Victor functions.
#define TRANSP_INTERLACED = 3 int save_a_gif(char *fname, imgdes *image) { int rcode; int savemode = TRANSP_INTERLACED; int transcolor = 43; rcode = savegifex(fname, image, savemode, transcolor); // Assumes LZW functionality previously unlocked return(rcode); }
Victor Image Processing Library homepage | Victor Application Notes